package cn.engage.org.service.impl;

import cn.engage.basic.service.impl.BaseServiceImpl;
import cn.engage.basic.util.MD5Utils;
import cn.engage.basic.util.PageList;
import cn.engage.basic.util.StrUtils;
import cn.engage.org.domain.Department;
import cn.engage.org.domain.Employee;
import cn.engage.org.mapper.EmployeeMapper;
import cn.engage.org.query.EmployeeQuery;
import cn.engage.org.service.EmployeeService;
import cn.engage.user.domain.Logininfo;
import cn.engage.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void add(Employee employee) {
        employee.setPassword("1");
        String randomString = StrUtils.getComplexRandomString(32);
        String md5password = MD5Utils.encrypByMd5(randomString + employee.getPassword());
        employee.setSalt(randomString);
        employee.setPassword(md5password);
        super.add(employee);
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        logininfoMapper.add(logininfo);
        // 添加角色表获取权限 employee.getErole().getId()
        employeeMapper.addRole(employee.getId(), 14l);
    }

    @Override
    public void update(Employee employee) {
        super.update(employee);
    }

    @Override
    public Employee findByLogininfoId(Long id) {
        return employeeMapper.findByLogininfoId(id);
    }

    @Override
    public List<Employee> findByShopId(Long shopId) {
        return employeeMapper.findByShopId(shopId);
    }
}
